(function () {
    var addressView = {};
    var addressTypeEle = null;
    var seaAirEle = null;
    var countryEle = null;
    var provinceEle = null;
    var cityEle = null;
    var streetEle = null;
    var areaEle = null;
    var divisionEle = null;
    var countryCode = 0;
    var parentId = 0;
    var originData = '';
    var service = null;
    var transportType = 0;
    var airSeaConfig = "";
    function clearNextAddress (currentEle) {
        var nextEle = currentEle.next;
        if (nextEle == null) {
            return;
        }
        nextEle.clearData();
        $('#city').val('');
        nextEle.parentId = null;
        return clearNextAddress(nextEle);
    }
    addressView.clearAllHomeCountrySelect = function () {
        clearNextAddress(provinceEle);
    };
    addressView.setService = function (addressService) {
        service = addressService;
    };
    addressView.showMiddle = function () {
        $('body').css({'overflow':'hidden', 'height':'100%'});
        $('.layer-cover').show();
        // $('.layer-content').scrollUnique();
    };

    addressView.hideMiddel = function () {
        $('body').css({'overflow':'auto', 'height':'auto'});
        $('#mobilephone, #email').removeClass("required");
        $('.layer-cover').hide();
    };
    addressView.setSeaAirList = function (airSeaConfigs, service, q, currentPage) {
        airSeaConfig = airSeaConfigs;
        var config = {
            urlParams: {
                countryId: airSeaConfig.countryCode,
                type: airSeaConfig.airSeaType,
                q: q || '',
                pageIndex: currentPage || 1,
                pageSize: '10'
            },
        };
        service.getSeaAirList(config, function (res) {
            if(airSeaConfig.airSeaType === 1){
                res.data.forEach(function(item){
                    if(item.englishName){
                        item.nameCode = item.name + '(' + item.englishName + ')';
                    }else{
                        item.nameCode = item.name;
                    }
                });
            }else{
                res.data.forEach(function(item){
                    if(item.triadCode){
                        item.nameCode = item.name + '(' + item.triadCode + ')';
                    }else{
                        item.nameCode = item.name;
                    }
                });
            }
            seaAirEle.setData(res);
        });
    };
    addressView.setPlaceHolder = function(typeName){
        seaAirEle.setSearchPlaceHoder(typeName);
    };
    addressView.setCountryList = function (service, q, currentPage) {
        if (!currentPage) {
            currentPage = 1;
        }
        var params = {
            urlParams: {
                q: q || '',
                pageIndex: currentPage,
                pageSize: '10',
            },
        };
        service.getCountryList(params, function (res) {
            if (!q) {
                originData = res.data;
            } else {
                countryEle.setDefaultCount(res.data.length);
            }
            res.data.forEach(function (item) {
                item.nameCode = item.name + '(' + item.figureCode + ')';
            });
            countryEle.setData(res);
        });
    };
    addressView.setProvinceList = function (service) {
        service.getProvinceList(
            {
                urlParams: {
                    countryCode: countryCode,
                    parentId: countryCode,
                    pageSize: 1000,
                },
            },
            function (res) {
                provinceEle.setData(res);
            }
        );
    };
    addressView.setCityList = function (service, q) {
        service.getCityList(
            {
                urlParams: {
                    q: q || '',
                    countryCode: countryCode,
                    parentId: cityEle.parentId,
                    pageSize:1000
                },
            },
            function (data) {
                if (data.data && data.data.length) {
                    cityEle.setData(data);
                } else {
                    cityEle.setData([]);
                }

            }
        );
    };
    addressView.setAreaList = function (service, q) {
        service.setAreaList(
            {
                urlParams: {
                    q: q || '',
                    countryCode: countryCode,
                    parentId: areaEle.parentId,
                    pageSize:1000
                },
            },
            function (data) {
                if (data.data && data.data.length) {
                    areaEle.setData(data);
                } else {
                    areaEle.setData([]);
                }

            }
        );
    };
    addressView.setStreetList = function (service, q) {
        service.setAreaList(
            {
                urlParams: {
                    q: q || '',
                    countryCode: countryCode,
                    parentId: streetEle.parentId,
                    pageSize:1000
                },
            },
            function (data) {
                if (data.data && data.data.length) {
                    streetEle.setData(data);
                } else {
                    streetEle.setData([]);
                }
            }
        );
    };
    addressView.setDivisionList = function (service, q, currentPage) {
        service.setDivisionList(
            {
                urlParams: {
                    q: q || '',
                    countryCode: countryCode,
                    parentId: countryCode,
                    pageIndex: currentPage,
                },
            },
            function (data) {
                divisionEle.setData(data);

                // if (data.data && data.data.length) {
                //     divisionEle.setData(data);
                // } else {
                //     divisionEle.setData([]);
                // }

            }
        );
    };
    addressView.initAddressTypeSelect = function(selectFactory, service, callback){
        var config = { urlParams:{ 'parentId':'0' } };

        service.getAddressType(config, function(res){
            if(res.errorCode === 0){
                addressTypeEle = selectFactory({
                    data: res,
                    id: 'transportTypeName',
                    defaultCount: 1000,
                    defaultText: '请选择',
                    showTextField: 'name',
                    isSaveInputVal: true,
                    attrTextModel: function (name, data, item) {
                        transportType = item.id;
                        callback(item);
                    },
                });
            }
        });
    };
    addressView.initSeaAirSelect = function(airSeaConfigs, selectFactory, service, q, callback){
        airSeaConfig = airSeaConfigs;
        var airSeaTypeName = airSeaConfig.airSeaType === 1 ? "请输入名称" : "请输入名称或三字码";
        seaAirEle = null;
        seaAirEle = selectFactory({
            data: [],
            isSearch: true,
            id: 'airSea',
            defaultText: '请选择',
            defaultCount: 1000,
            showTextField: 'nameCode',
            searchPlaceHoder: airSeaTypeName,
            isSaveInputVal: true,
            closeLocalSearch: true,
            pagination: true,
            onSearchValueChange: function (attachEle, q, currentPage) {
                $("#mobilephone").removeClass("required");
                addressView.setSeaAirList(airSeaConfig, service, q, currentPage);
            },
            attrTextModel: function (name, data, item) {
                callback(item);
            },
        });
    };
    addressView.initCountrySelect = function (selectFactory, service, q, callback) {
        countryEle = selectFactory({
            data: [],
            isSearch: true,
            id: 'address',
            defaultText: '请选择',
            defaultCount: 1000,
            showTextField: 'nameCode',
            searchPlaceHoder: '请输入国家名称或二字码',
            isSaveInputVal: true,
            closeLocalSearch: true,
            pagination: true,
            onSearchValueChange: function (attachEle, q, currentPage) {
                $("#mobilephone").removeClass("required");
                addressView.setCountryList(service, q, currentPage);
            },
            attrTextModel: function (name, data, currentData) {
                airSeaConfig.countryCode = currentData.code;
                // addressView.initSeaAirSelect(airSeaConfig, selectFactory, service, '', 1);
                countryCode = currentData.figureCode;
                addressView.setProvinceList(service);
                callback(currentData.name, countryCode);
            },
        });
    };
    addressView.initProvinceSelect = function (selectFactory, service, callback) {
        provinceEle = selectFactory({
            data: [],
            id: 'province',
            defaultText: '请选择',
            defaultCount: 1000,
            isSaveInputVal: true,
            attrTextModel: function (name, data, currentData) {
                $("#mobilephone").removeClass("required");
                clearNextAddress(provinceEle);
                cityEle.parentId = currentData.id;
                if (name) {
                    addressView.setCityList(service);
                    cityEle.open()

                }
                callback(name, currentData.id);
            },
        });
    };
    addressView.initCitySelect = function (selectFactory, service, callback) {
        cityEle = selectFactory({
            data: [],
            id: 'city',
            defaultText: '请选择',
            defaultCount: 1000,
            isSaveInputVal: true,
            attrTextModel: function (name, data, currentData) {
                $("#mobilephone").removeClass("required");
                clearNextAddress(cityEle);
                areaEle.parentId = currentData.id;
                if (name) {
                    addressView.setAreaList(service);
                    areaEle.open();
                }
                callback(name, currentData.id);
            },
        });
        provinceEle.next = cityEle;
    };
    addressView.initAreaSelect = function (selectFactory, service, callback) {
        areaEle = selectFactory({
            data: [],
            id: 'area',
            defaultText: '请选择',
            isSaveInputVal: true,
            maxHeight: 165,
            defaultCount: 1000,
            attrTextModel: function (name, data, currentData) {
                $("#mobilephone").removeClass("required");
                clearNextAddress(areaEle);
                streetEle.parentId = currentData.id;
                addressView.setStreetList(service);
                streetEle.open();
                callback(name, currentData.id);
            },
        });
        cityEle.next = areaEle;
    };
    addressView.initStreetSelect = function (selectFactory, service, callback) {
        $("#mobilephone").removeClass("required");
        streetEle = selectFactory({
            data: [],
            id: 'street',
            defaultText: '请选择',
            isSaveInputVal: true,
            maxHeight: 165,
            defaultCount: 1000,
            attrTextModel: function (name, data, currentData) {
                callback(name, currentData.id);
            },
        });
        areaEle.next = streetEle;
    };
    addressView.initDivisionSelect = function (selectFactory, service, callback) {
        divisionEle = selectFactory({
            data: [],
            id: 'division',
            defaultText: '请选择',
            defaultCount: 1000,
            searchPlaceHoder: '请输入地区名称',
            isSearch: true,
            isSaveInputVal: true,
            pagination: true,
            maxHeight: 165,
            onSearchValueChange: function (attachEle, q, currentPage) {
                $("#mobilephone").removeClass("required");
                currentPage = currentPage ? currentPage : 1;
                addressView.setDivisionList(service, q, currentPage);
            },
            attrTextModel: function (name, data, currentData) {
                callback(name, currentData.id);
            }
        });
    };
    addressView.setAddressId = function (editData) {
        countryCode = editData.countryCode;
        cityEle.parentId = editData.provinceId;
        areaEle.parentId = editData.cityId;
        streetEle.parentId = editData.districtId;
        if (editData.provinceId) {
            addressView.setProvinceList(service, '');
        }
        if (editData.cityId) {
            addressView.setCityList(service, '');
        }
        if (editData.districtId) {
            addressView.setAreaList(service, '');
        }
        if (editData.streetId) {
            addressView.setStreetList(service, '');
        }
        if (editData.countryCode && editData.countryCode.toLowerCase() != 'cn') {
            addressView.setDivisionList(service, '');
        }
    };
    addressView.closeInputAutoComplete = function () {
        var keys = ['name', 'address', 'province', 'city', 'area', 'street', 'division', 'addressDetail', 'phone', 'postcode', 'email'];
        for (var index = 0; index < keys.length; index++) {
            $('#' + keys[index]).attr('autocomplete', 'off');
        }
    };
    addressView.save = function (data, service, callback) {
        var type = easyMe.getQueryString('type');
        switch(type){
            case 'send': type = 1; break;
            case 'receive': type = 2; break;
            case 'pick': type = 3; break;
        }
        var params = {
            name: data.name,
            phone: data.mobilephone,
            telPhone: data.telephone,
            email: $.trim(data.email),
            address: data.addressDetail,
            countryCode: data.countryCode,
            provinceId: data.provinceId,
            cityId: data.cityId,
            districtId: data.districtId,
            streetId: data.streetId,
            postcode: data.postcode,
            isDefault: data.isDefault,
            type: type,
            transportType: data.transportType,
            transportId: data.airSeaId == "" ? undefined : data.airSeaId,
        };
        if (!data.id) {
            // 新增
            service.saveAddress(params, callback);
        } else {
            // 更新
            params.id = data.id;
            service.updateAddress(params, callback);
        }
    };
    addressView.setAutoCompleteOff = function () {
        var keys = ['name', 'address', 'province', 'city', 'area', 'street', 'division', 'addressDetail', 'mobilephone', 'telephone', 'postcode', 'email', 'airSea'];
        for (var index = 0; index < keys.length; index++) {
            $('#' + keys[index]).attr('autocomplete', 'off');
        }
    };
    addressView.setDefaultAddress = function (type, id, transportType, service, callback) {
        var params = {
            type: type,
            newId: id,
            transportType: transportType
        };
        service.setDefaultAddress(params, callback);
    };
    module.exports = addressView;
})();
